package com.mulanweb.api.config;

import com.jfinal.config.*;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.IErrorRenderFactory;
import com.jfinal.render.JsonRender;
import com.jfinal.render.Render;
import com.jfinal.render.ViewType;
import com.mulanweb.api.module.controller.*;
import com.mulanweb.api.module.controller.CharteredController;
import com.mulanweb.api.module.controller.OrderController;
import com.mulanweb.api.module.controller.TicketController;
import com.mulanweb.api.module.controller.VisaOrderController;
import com.mulanweb.api.module.controller.admin.AdminUserController;
import com.mulanweb.api.module.controller.admin.CityController;
import com.mulanweb.api.module.controller.admin.ExtensionPersonnelController;
import com.mulanweb.api.module.controller.admin.HotCommodityController;
import com.mulanweb.api.module.controller.event.EventController;
import com.mulanweb.api.module.controller.event.PhotographyController;
import com.mulanweb.api.module.controller.seller.SellerContentController;
import com.mulanweb.api.module.controller.seller.VerifyController;
import com.mulanweb.api.module.controller.wechat.*;
import com.mulanweb.api.module.model._MappingKit;
import com.mulanweb.handler.*;
import com.mulanweb.handler.Timer.DayRunTimer;
import com.mulanweb.handler.wechat.JsApiTicketTimerHandler;
import com.mulanweb.handler.wechat.TokenTimerHandler;
import com.mulanweb.interceptor.ValidatorInterceptor;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * Created by hcy on 2016/4/8.
 */
public class AppConfig extends JFinalConfig {
    public static final String MULAN_XXL = "mulan_xxl";

    @Override
    public void configConstant(Constants me) {

        System.setProperty("javax.xml.parsers.SAXParserFactory", "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
        me.setDevMode(true);
        me.setViewType(ViewType.JSP);
        me.setJsonFactory(new FastJsonFactory());
        me.setBaseUploadPath("upload" + File.separator + "image");
        me.setJsonDatePattern("yyyy-MM-dd HH:mm:ss");
        me.setErrorRenderFactory(new IErrorRenderFactory() {
            @Override
            public Render getRender(int errorCode, String view) {
                switch (errorCode) {
                    case 404:
                        return new JsonRender("{\"ReturnCode\": 404, \"AlertMessage\": \"无效的请求地址\"}");
                    case 500:
                        return new JsonRender("{\"ReturnCode\": 500, \"AlertMessage\": \"服务器处理异常\"}");
                    default:
                        return new JsonRender("{\"ReturnCode\": 999, \"AlertMessage\": \"未知错误\"}");
                }
            }
        });
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/",IndexController.class);
        me.add("/admin/test", WxManagerController.class);
        me.add("/mulanweb/evaluate", ContentEvaluateController.class);
        me.add("/admin/createMenu", MenuController.class);
        me.add("/mulanweb/wecharPay", SignatureController.class);
        me.add("/mulanweb/wxpay", WxPayController.class);

        //admin 系列为后台管理程序接口
        me.add("/mulanweb/admin/photo", com.mulanweb.api.module.controller.admin.PhotoController.class);
        me.add("/mulanweb/admin/seller", com.mulanweb.api.module.controller.admin.SellerController.class);
        me.add("/mulanweb/admin/coupon", CouponController.class);
        me.add("/mulanweb/admin/scenic_spot", ScenicSpotController.class);
        me.add("/mulanweb/admin/car_brand", CarBrandController.class);
        me.add("/mulanweb/admin/admin_user", AdminUserController.class);
        me.add("/mulanweb/admin/chat_history", com.mulanweb.api.module.controller.admin.ChatHistoryController.class);
        me.add("/mulanweb/admin/extension_personnel", ExtensionPersonnelController.class);

        me.add("/mulanweb/common/login_out", LoginController.class);
        me.add("/mulanweb/common/ticket", TicketController.class);
        me.add("/mulanweb/common/chartered", CharteredController.class);
        me.add("/mulanweb/common/order", OrderController.class);
        me.add("/mulanweb/common/visa_order", VisaOrderController.class);
        me.add("/mulanweb/common/hot_commodity", com.mulanweb.api.module.controller.HotCommodityController.class);
        me.add("/mulanweb/common/city", com.mulanweb.api.module.controller.CityController.class);
        me.add("/mulanweb/common/info_collect", InfoCollectController.class);
        me.add("/mulanweb/common/photo", com.mulanweb.api.module.controller.PhotoController.class);
        me.add("/mulanweb/common/collection", com.mulanweb.api.module.controller.CollectionController.class);
        me.add("/mulanweb/common/seller", com.mulanweb.api.module.controller.SellerController.class);
        me.add("/mulanweb/common/visa",com.mulanweb.api.module.controller.VisaController.class);
        me.add("/mulanweb/common/searchHistory",com.mulanweb.api.module.controller.VisaController.class);

        me.add("/mulanweb/admin/user", com.mulanweb.api.module.controller.admin.UserController.class);
        me.add("/mulanweb/admin/ticket", com.mulanweb.api.module.controller.admin.TicketController.class);
        me.add("/mulanweb/admin/chartered", com.mulanweb.api.module.controller.admin.CharteredController.class);
        me.add("/mulanweb/admin/order", com.mulanweb.api.module.controller.admin.OrderController.class);
        me.add("/mulanweb/admin/visa_order", com.mulanweb.api.module.controller.admin.VisaOrderController.class);
        me.add("/mulanweb/admin/hot_commodity", HotCommodityController.class);
        me.add("/mulanweb/admin/city",CityController.class);

        me.add("/mulanweb/admin/visa",com.mulanweb.api.module.controller.admin.VisaController.class);

        me.add("/mulanweb/wechat/visa_order",com.mulanweb.api.module.controller.wechat.VisaOrderController.class);
        me.add("/mulanweb/wechat/order",com.mulanweb.api.module.controller.wechat.OrderController.class);
        me.add("/mulanweb/wechat/refund", RefundController.class);
        me.add("/mulanweb/wechat/collection", com.mulanweb.api.module.controller.wechat.CollectionController.class);

        me.add("/mulanweb/seller/verify",VerifyController.class);
        me.add("/mulanweb/seller/content",SellerContentController.class);

        me.add("/mulanweb/event", EventController.class);
        me.add("/mulanweb/event/photography", PhotographyController.class);
     }

    public void configPlugin(Plugins plugins) {
        Prop prop = PropKit.use("app.config");
        String url = prop.get("jdbc.url"),
                user = prop.get("jdbc.user"),
                password = prop.get("jdbc.password");

        DruidPlugin dp = new DruidPlugin(url, user, password);
        plugins.add(dp);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);

        _MappingKit.mapping(arp);

        plugins.add(arp);

        EhCachePlugin ehCachePlugin = new EhCachePlugin();
        plugins.add(ehCachePlugin);
    }

    @Override
    public void configInterceptor(Interceptors me) {
        //配置验证参数拦截器
        me.add(new ValidatorInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {
       // me.add(new MulanHttpServletRequestHandler());
         me.add(new LoginVerifyHandler());
    }
    
    @Override
    public void afterJFinalStart() {
        java.util.Timer timer = new java.util.Timer();
        timer.schedule(new TokenTimerHandler(), 0, TimeUnit.MINUTES.toMillis(10));

        java.util.Timer timer2 = new java.util.Timer();
        timer2.schedule(new JsApiTicketTimerHandler(), 0, TimeUnit.MINUTES.toMillis(20));

        java.util.Timer timer3 = new java.util.Timer();
        timer3.schedule(new DayRunTimer(), 0, TimeUnit.HOURS.toMillis(24));

        /*java.util.Timer timer3 = new java.util.Timer();
        timer3.schedule(new QuerySellerPayment(), 0, TimeUnit.MINUTES.toMillis(30));*/

       /* String baseModelPackageName = "com.mulanweb.api.module.model.base" ;
        String baseModelOutputDir = "D:\\temp\\base";
        String modelPackageName = "com.mulanweb.api.module.model";
        String modelOutputDir = "D:\\temp";

        Generator generator = new Generator(DbKit.getConfig().getDataSource(),
                    baseModelPackageName,baseModelOutputDir,modelPackageName,modelOutputDir);
        generator.generate();*/
    }
}







